#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    pid_t id = fork();
    //子进程
    if(id == 0)
    {
        printf("开始\n");
        sleep(1);

        //这里的两个ls意义不同，第一个是告诉系统要找谁，第二个是用来表明执行什么命令
        //execlp("ls","ls","-a","-l","--color=auto",NULL);

        //execl("/usr/bin/ls","ls","-a","-l","--color=auto",NULL);
        //execl("/usr/bin/lsssssd","ls","-a","-l","--color=auto",NULL);
        
        //char* const arr[] = {"ls","-a","-l","--color=auto",NULL};                                                                                         
        //execv("/usr/bin/ls",arr);
        //execvp("ls",arr);
        //调用自己的可执行程序
        //execl("./mytest","./mytest",NULL);
        
        //调用其他语言的可执行程序
        //execl("./mycpp","./mycpp",NULL);
        
        //char* const _envp[] = {(char*)"MYVAL=111222333",NULL};
        //execle("./mytest","mytest",NULL,_envp); //自定义环境变量

        extern char** environ;
        putenv((char*)"MYVAL=44332211");  //将指定环境变量导入到系统中，environ指向的环境变量表
        execle("./mytest","mytest",NULL,environ);//实际上不传environ，子进程也能获取


        perror("execl");
        exit(1);
    }

    //父进程，这里的id就是子进程的id
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0)
    {
        printf("wait success! exit code: %d, sig: %d\n",(status >> 8) & 0xFF, status & 0x7F);
    }
    //printf("开始\n");
    //execl("/usr/bin/ls","ls","--color=auto","-a","-l",NULL);
    //printf("执行完成\n");

    return 0;
}
